package com.ddyx.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ddyx.entity.Goods;
import com.ddyx.mapper.BookingMapper;
import com.ddyx.mapper.CustomerMapper;
import com.ddyx.mapper.EventClickPageviewMapper;
import com.ddyx.mapper.GoodsMapper;
import com.ddyx.mapper.WxUserMapper;
import com.ddyx.service.VipService;
import com.ddyx.util.CalculateUtil;
import com.ddyx.util.DateUtil;
import com.ddyx.util.MathUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class VipServiceImpl implements VipService {
	public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	
	ObjectMapper jsonUtil = new ObjectMapper();
	
	@Resource
	CustomerMapper customerMapper;

	@Resource
	EventClickPageviewMapper ecpMapper;

	@Resource
	BookingMapper bookingMapper;

	@Resource
	GoodsMapper goodsMapper;
	
	@Resource
	WxUserMapper wuMapper;

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm visitorAndGoodsCount(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param.getData() != null) {
			Map paramMap = (Map) param.getData();
			paramMap.put("startTime", DateUtil.toDate(paramMap.get("startTime")));
			paramMap.put("endTime", DateUtil.toDate(paramMap.get("endTime")));
			String storeNo = (String) paramMap.get("storeNo");
			Date endTime = new Date();
			Date startTime = CalculateUtil.dayChange(endTime, -90);
			try {

				/* 总访客 */
				Long vCount = ecpMapper.getVisitorCount(paramMap);
				/* 新访客 */
				Long nvCount = ecpMapper.getNewVisitorCount(paramMap);
				/* 预约人数 */
				Long bCount = bookingMapper.getBookingCount(paramMap);
				/* 90天内预约超过两次的客户数 */
				Long b2Count = bookingMapper.getBooking2Count(storeNo, startTime, endTime);
				/* 90天内浏览商品的客户数 */
				Long pvgCount = ecpMapper.getPvgCount(storeNo, startTime, endTime);
				/* 转化率 */
				String changeRate = MathUtil.decimal2point(bCount, vCount);
				/* 回头率 */
				String returnRate = MathUtil.decimal2point(b2Count, pvgCount);
				/* 上架商品总数 */
				Map map = new HashMap<>();
				map.put("storeNo", (String) paramMap.get("storeNo"));
				Long sogCount = goodsMapper.getSoldonCount(map);
				/* 新上架商品总数 */
				Long nsogCount = goodsMapper.getSoldonCount(paramMap);
				Map resultMap = new HashMap<>();
				resultMap.put("vCount", vCount);
				resultMap.put("nvCount", nvCount);
				resultMap.put("bCount", bCount);
				resultMap.put("changeRate", changeRate);
				resultMap.put("returnRate", returnRate);
				resultMap.put("sogCount", sogCount);
				resultMap.put("nsogCount", nsogCount);
				
				result.setData(resultMap);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getLookGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			LinkedHashMap resultMap = new LinkedHashMap<>();
//			List<Goods> goodsList = customerMapper.getLookGoodsCusts(paramMap);
			//获取时间轴timeLine
			try {
				List<Date> timeLines = ecpMapper.getTimeLines(paramMap);
				SimpleDateFormat dateFromat = new SimpleDateFormat("yyyy-MM-dd");
				if(timeLines!=null) {
					for (Date timeLine : timeLines) {
						paramMap.put("timeLine", dateFromat.format(timeLine));
						List<Goods> lookGoods = customerMapper.getLookGoods(paramMap);
						if(lookGoods!=null)
							resultMap.put(timeLine, lookGoods);
					}
				}
				result.setData(resultMap);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
			}
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm getCartogram(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			String storeNo = (String) paramMap.get("storeNo");
			String actionType = (String) paramMap.get("actionType");
			/*本期和上期时间轴*/
			List currentPeriodDay = new ArrayList<>();
			List priorPeriodDay = new ArrayList<>();
			Map resultMap = new HashMap<>();
			for (int i = 0; i < 7; i++) {
				currentPeriodDay.add(dateFormat.format(CalculateUtil.dayChange(new Date(),-i)));
				priorPeriodDay.add(dateFormat.format(CalculateUtil.dayChange(new Date(),-(i+7))));
			}
			try {
				if("v".equalsIgnoreCase(actionType)) {
					/*本期访客数集合*/
					List visitorCountList = new ArrayList<>();
					/*环比发展集合*/
					List momDevelopList = new ArrayList<>();
					for (int i = 0; i < 7; i++) {
						Long currentVisitorCount = ecpMapper.getVisitorCountByDay(storeNo, currentPeriodDay.get(i));
						visitorCountList.add(currentVisitorCount);
						Long priorVisitorCount = ecpMapper.getVisitorCountByDay(storeNo, priorPeriodDay.get(i));
						momDevelopList.add(MathUtil.decimal2point(currentVisitorCount, priorVisitorCount));
					}
					resultMap.put("visitorCountList", visitorCountList);
					resultMap.put("momDevelopList", momDevelopList);
				}
				if("b".equalsIgnoreCase(actionType)) {
					/*本期预约数集合*/
					List bookingCountList = new ArrayList<>();
					/*环比增长集合*/
					List momGrowList = new ArrayList<>();
					for (int i = 0; i < 7; i++) {
						Long currentBookingCount = bookingMapper.getBookingCountByDay(storeNo, currentPeriodDay.get(i));
						bookingCountList.add(currentBookingCount);
						Long priorBookingCount = bookingMapper.getBookingCountByDay(storeNo, priorPeriodDay.get(i));
						momGrowList.add(MathUtil.decimal2point((currentBookingCount-priorBookingCount), priorBookingCount));
					}
					resultMap.put("bookingCountList", bookingCountList);
					resultMap.put("momGrowList", momGrowList);
				}
				if("v_b".equalsIgnoreCase(actionType)) {
					/*本期预约转化率集合*/
					List changeRateList = new ArrayList<>();
					/*环比增长集合*/
					List momGrowList = new ArrayList<>();
					for (int i = 0; i < 7; i++) {
						Long currentVisitorCount = ecpMapper.getVisitorCountByDay(storeNo, currentPeriodDay.get(i));
						Long currentBookingCount = bookingMapper.getBookingCountByDay(storeNo, currentPeriodDay.get(i));
						changeRateList.add(MathUtil.decimal2point(currentVisitorCount, currentBookingCount));
						Long priorVisitorCount = ecpMapper.getVisitorCountByDay(storeNo, priorPeriodDay.get(i));
						Long priorBookingCount = bookingMapper.getBookingCountByDay(storeNo, priorPeriodDay.get(i));
						Long cv_pb = currentVisitorCount/(currentBookingCount>0?currentBookingCount:1);
						Long pv_pb = priorVisitorCount/(priorBookingCount>0?priorBookingCount:1);
						momGrowList.add(MathUtil.decimal2point(cv_pb, pv_pb));
						resultMap.put("changeRateList", changeRateList);
						resultMap.put("momGrowList", momGrowList);
					}
				}
				result.setData(resultMap);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
			}
		}
		
		return result;
	}

	
	
	/**
	 * @desc vip助手获取访客的详细信息
	 * @param param
	 * @return
	 */
	@Override
	public ResponseForm getVisitorList(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			try {
				paramMap.put("startTime", DateUtil.toDate(paramMap.get("startTime")));
				paramMap.put("endTime", DateUtil.toDate(paramMap.get("endTime")));
				com.ddyx.util.Page.parsePage(paramMap);
				Page page = PageHelper.startPage((int)paramMap.get("page"),(int) paramMap.get("size"));
				List visitorList = ecpMapper.getVisitorList(paramMap);
				result.setData(visitorList);
				result.setTotal((int)page.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
			}
		}
		return result;
	}

	
	/**
	 * @desc vip助手获取预约用户的详细信息
	 * @param param
	 * @return
	 */
	@Override
	public ResponseForm getBookerList(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			try {
				paramMap.put("startTime", DateUtil.toDate(paramMap.get("startTime")));
				paramMap.put("endTime", DateUtil.toDate(paramMap.get("endTime")));
//				paramMap.put("startTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Long)paramMap.get("startTime")));
//				paramMap.put("endTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Long)paramMap.get("endTime")));
				com.ddyx.util.Page.parsePage(paramMap);
				Page page = PageHelper.startPage((int)paramMap.get("page"),(int) paramMap.get("size"));
				List bookerList = bookingMapper.getBookerList(paramMap);
				result.setData(bookerList);
				result.setTotal((int)page.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getBrowsedGoods(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			try {
				paramMap.put("startTime", DateUtil.toDate(paramMap.get("startTime")));
				paramMap.put("endTime", DateUtil.toDate(paramMap.get("endTime")));
				com.ddyx.util.Page.parsePage(paramMap);
				Page page = PageHelper.startPage((int)paramMap.get("page"),(int) paramMap.get("size"));
				List browsedGoods = goodsMapper.getBrowsedGoods(paramMap);
				result.setData(browsedGoods);
				result.setTotal((int)page.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getCustomersByGood(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			try {
				paramMap.put("startTime", DateUtil.toDate(paramMap.get("startTime")));
				paramMap.put("endTime", DateUtil.toDate(paramMap.get("endTime")));
				com.ddyx.util.Page.parsePage(paramMap);
				Page page = PageHelper.startPage((int)paramMap.get("page"),(int) paramMap.get("size"));
				List customers = wuMapper.getCustomersByGood(paramMap);
				result.setData(customers);
				result.setTotal((int)page.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getGoodsByCustomer(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			try {
				paramMap.put("startTime", DateUtil.toDate(paramMap.get("startTime")));
				paramMap.put("endTime", DateUtil.toDate(paramMap.get("endTime")));
				com.ddyx.util.Page.parsePage(paramMap);
				Page page = PageHelper.startPage((int)paramMap.get("page"),(int) paramMap.get("size"));
				List goodsList = goodsMapper.getGoodsByCustomer(paramMap);
				result.setData(goodsList);
				result.setTotal((int)page.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getGoodsByBooker(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			try {
				paramMap.put("startTime", DateUtil.toDate(paramMap.get("startTime")));
				paramMap.put("endTime", DateUtil.toDate(paramMap.get("endTime")));
				com.ddyx.util.Page.parsePage(paramMap);
				Page page = PageHelper.startPage((int)paramMap.get("page"),(int) paramMap.get("size"));
				List goodsList = goodsMapper.getGoodsByBooker(paramMap);
				result.setData(goodsList);
				result.setTotal((int)page.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getBookingGoodsByStore(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			try {
				paramMap.put("startTime", DateUtil.toDate(paramMap.get("startTime")));
				paramMap.put("endTime", DateUtil.toDate(paramMap.get("endTime")));
				com.ddyx.util.Page.parsePage(paramMap);
				Page page = PageHelper.startPage((int)paramMap.get("page"),(int) paramMap.get("size"));
				List goodsList = goodsMapper.getBookingGoodsByStore(paramMap);
				result.setData(goodsList);
				result.setTotal((int)page.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getBookersByGood(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if(param.getData()!=null) {
			Map paramMap = (Map) param.getData();
			try {
				paramMap.put("startTime", DateUtil.toDate(paramMap.get("startTime")));
				paramMap.put("endTime", DateUtil.toDate(paramMap.get("endTime")));
				com.ddyx.util.Page.parsePage(paramMap);
				Page page = PageHelper.startPage((int)paramMap.get("page"),(int) paramMap.get("size"));
				List bookers = wuMapper.getBookersByGood(paramMap);
				result.setData(bookers);
				result.setTotal((int)page.getTotal());
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("参数有误");
				log.error("参数有误");
			}
		}
		return result;
	}

}
